---
- hosts: "masters, workers"
  remote_user: ubuntu
  become: yes
  become_method: sudo
  become_user: root
  gather_facts: yes
  connection: ssh
  
  tasks:
   
   - name: Make the Swap inactive
     command: swapoff -a

   - name: Remove Swap entry from /etc/fstab.
     lineinfile:
       dest: /etc/fstab
       regexp: swap
       state: absent

   - name: Create a empty file for containerd module.
     copy:
       content: ""
       dest: /etc/modules-load.d/containerd.conf
       force: no

   - name: Configure module for containerd.
     blockinfile:
       path: /etc/modules-load.d/containerd.conf 
       block: |
            overlay
            br_netfilter

   - name: Create a empty file for kubernetes sysctl params.
     copy:
       content: ""
       dest: /etc/sysctl.d/99-kubernetes-cri.conf
       force: no

   - name: Configure sysctl params for Kubernetes.
     lineinfile:
       path: /etc/sysctl.d/99-kubernetes-cri.conf 
       line: "{{ item }}"
     with_items:
       - 'net.bridge.bridge-nf-call-iptables  = 1'
       - 'net.ipv4.ip_forward                 = 1'
       - 'net.bridge.bridge-nf-call-ip6tables = 1'

   - name: Apply sysctl params without reboot.
     command: sysctl --system

   - name: Installing Prerequisites for Kubernetes
     apt: 
       name:
         - apt-transport-https
         - ca-certificates
         - curl
         - gnupg-agent
         - vim
         - software-properties-common
       state: present

   - name: Add Docker’s official GPG key
     apt_key:
       url: https://download.docker.com/linux/ubuntu/gpg
       state: present

   - name: Add Docker Repository
     apt_repository:
       repo: deb [arch=amd64] https://download.docker.com/linux/ubuntu xenial stable
       state: present
       filename: docker
       update_cache: yes

   - name: Install containerd.
     apt: 
       name:
         - containerd.io
       state: present

   - name: Configure containerd.
     file:
       path: /etc/containerd
       state: directory

   - name: Configure containerd.
     shell: /usr/bin/containerd config default > /etc/containerd/config.toml

   - name: Enable containerd service, and start it.
     systemd: 
       name: containerd
       state: restarted
       enabled: yes
       daemon-reload: yes

   - name: Add Google official GPG key
     apt_key:
       url: https://packages.cloud.google.com/apt/doc/apt-key.gpg
       state: present

   - name: Add Kubernetes Repository
     apt_repository:
       repo: deb http://apt.kubernetes.io/ kubernetes-xenial main 
       state: present
       filename: kubernetes
       mode: 0600

   - name: Installing Kubernetes Cluster Packages.
     apt: 
       name:
         - kubeadm
         - kubectl
         - kubelet
       state: present

   - name: Enable service kubelet, and enable persistently
     service: 
       name: kubelet
       enabled: yes

   - name: Reboot all the kubernetes nodes.
     reboot:
       post_reboot_delay: 10
       reboot_timeout: 40
       connect_timeout: 60
       test_command: uptime

   - pause: seconds=20

- hosts: masters
  remote_user: ubuntu
  become: yes
  become_method: sudo
  become_user: root
  gather_facts: yes
  connection: ssh

  vars_prompt:

   - name: "pod_network_cidr"
     prompt: "Enter the Pod Network CIDR, example: 192.168.100.0/24"
     private: no

   - name: "k8s_master_ip"
     prompt: "Enter the Apiserver advertise address, example: 192.168.0.26"
     private: no

  tasks:

   - name: Intilizing Kubernetes Cluster
     command: kubeadm init --pod-network-cidr "{{ pod_network_cidr }}"  --apiserver-advertise-address "{{ k8s_master_ip }}"
     run_once: true
     delegate_to: "{{ k8s_master_ip }}"

   - pause: seconds=30

   - name: Create directory for kube config.
     become_user: ubuntu
     become_method: sudo
     become: yes
     file: 
       path: /home/{{ansible_user }}/.kube
       state: directory
       owner: "{{ ansible_user }}"
       group: "{{ ansible_user }}"
       mode: 0755

   - name: Copy /etc/kubernetes/admin.conf to user home directory /home/{{ ansible_user }}/.kube/config.
     become_user: root
     become_method: sudo
     become: yes
     copy:
       src: /etc/kubernetes/admin.conf
       dest: /home/{{ ansible_user }}/.kube/config
       remote_src: yes
       owner: "{{ ansible_user }}"
       group: "{{ ansible_user }}"
       mode: '0644'

   - pause: seconds=10

   - name: Remove the cache directory.
     become_user: ubuntu
     become_method: sudo
     become: yes
     file: 
       path: /home/{{ ansible_user }}/.kube/cache
       state: absent

   - name: Create Pod Network 
     become_user: ubuntu
     become_method: sudo
     become: yes
     shell: kubectl apply -f https://docs.projectcalico.org/manifests/calico.yaml >> pod_network_setup.txt
     args:
       chdir: $HOME
       creates: pod_network_setup.txt

   - pause: seconds=30

   - name: Get the token for joining the nodes with Kuberentes master.
     shell: kubeadm token create  --print-join-command
     register: kubernetes_join_command

   - debug:
       msg: "{{ kubernetes_join_command.stdout }}"

   - name: Copy join command to local file.
     become: false
     local_action: copy content="{{ kubernetes_join_command.stdout_lines[0] }}" dest="/tmp/kubernetes_join_command" mode=0777

- hosts: workers
  remote_user: ubuntu
  become: yes
  become_method: sudo
  become_user: root
  gather_facts: yes
  connection: ssh
       
  tasks:

   - name: Copy join command to worker nodes.
     become: yes
     become_method: sudo
     become_user: root
     copy:
       src: /tmp/kubernetes_join_command
       dest: /tmp/kubernetes_join_command
       mode: 0777   

   - name: Join the Worker nodes with master.
     become: yes
     become_method: sudo
     become_user: root
     command: sh /tmp/kubernetes_join_command
     register: joined_or_not

   - debug:
       msg: "{{ joined_or_not.stdout }}"

- hosts: masters
  remote_user: ubuntu
  become: yes
  become_method: sudo
  become_user: root
  gather_facts: yes
  connection: ssh
       
  tasks:

   - name: Configure kubectl command auto completion.
     lineinfile:
       dest: /home/{{ ansible_user }}/.bashrc
       line: 'source <(kubectl completion bash)'
       insertafter: EOF
...